home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1J153LS (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.4 KB  |  234 lines

  1. package java.io;
  2.  
  3. public class DataInputStream extends FilterInputStream implements DataInput {
  4.    private char[] lineBuffer;
  5.  
  6.    public DataInputStream(InputStream in) {
  7.       super(in);
  8.    }
  9.  
  10.    public final int read(byte[] b) throws IOException {
  11.       return super.in.read(b, 0, b.length);
  12.    }
  13.  
  14.    public final int read(byte[] b, int off, int len) throws IOException {
  15.       return super.in.read(b, off, len);
  16.    }
  17.  
  18.    public final boolean readBoolean() throws IOException {
  19.       int ch = super.in.read();
  20.       if (ch < 0) {
  21.          throw new EOFException();
  22.       } else {
  23.          return ch != 0;
  24.       }
  25.    }
  26.  
  27.    public final byte readByte() throws IOException {
  28.       int ch = super.in.read();
  29.       if (ch < 0) {
  30.          throw new EOFException();
  31.       } else {
  32.          return (byte)ch;
  33.       }
  34.    }
  35.  
  36.    public final char readChar() throws IOException {
  37.       InputStream in = super.in;
  38.       int ch1 = in.read();
  39.       int ch2 = in.read();
  40.       if ((ch1 | ch2) < 0) {
  41.          throw new EOFException();
  42.       } else {
  43.          return (char)((ch1 << 8) + (ch2 << 0));
  44.       }
  45.    }
  46.  
  47.    public final double readDouble() throws IOException {
  48.       return Double.longBitsToDouble(this.readLong());
  49.    }
  50.  
  51.    public final float readFloat() throws IOException {
  52.       return Float.intBitsToFloat(this.readInt());
  53.    }
  54.  
  55.    public final void readFully(byte[] b) throws IOException {
  56.       this.readFully(b, 0, b.length);
  57.    }
  58.  
  59.    public final void readFully(byte[] b, int off, int len) throws IOException {
  60.       InputStream in = super.in;
  61.  
  62.       int count;
  63.       for(int n = 0; n < len; n += count) {
  64.          count = in.read(b, off + n, len - n);
  65.          if (count < 0) {
  66.             throw new EOFException();
  67.          }
  68.       }
  69.  
  70.    }
  71.  
  72.    public final int readInt() throws IOException {
  73.       InputStream in = super.in;
  74.       int ch1 = in.read();
  75.       int ch2 = in.read();
  76.       int ch3 = in.read();
  77.       int ch4 = in.read();
  78.       if ((ch1 | ch2 | ch3 | ch4) < 0) {
  79.          throw new EOFException();
  80.       } else {
  81.          return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0);
  82.       }
  83.    }
  84.  
  85.    /** @deprecated */
  86.    public final String readLine() throws IOException {
  87.       InputStream in = super.in;
  88.       char[] buf = this.lineBuffer;
  89.       if (buf == null) {
  90.          buf = this.lineBuffer = new char[128];
  91.       }
  92.  
  93.       int room = buf.length;
  94.       int offset = 0;
  95.  
  96.       while(true) {
  97.          int c;
  98.          switch (c = in.read()) {
  99.             case 13:
  100.                int c2 = in.read();
  101.                if (c2 != 10) {
  102.                   if (!(in instanceof PushbackInputStream)) {
  103.                      in = super.in = new PushbackInputStream(in);
  104.                   }
  105.  
  106.                   ((PushbackInputStream)in).unread(c2);
  107.                }
  108.             case -1:
  109.             case 10:
  110.                if (c == -1 && offset == 0) {
  111.                   return null;
  112.                }
  113.  
  114.                return String.copyValueOf(buf, 0, offset);
  115.          }
  116.  
  117.          --room;
  118.          if (room < 0) {
  119.             buf = new char[offset + 128];
  120.             room = buf.length - offset - 1;
  121.             System.arraycopy(this.lineBuffer, 0, buf, 0, offset);
  122.             this.lineBuffer = buf;
  123.          }
  124.  
  125.          buf[offset++] = (char)c;
  126.       }
  127.    }
  128.  
  129.    public final long readLong() throws IOException {
  130.       InputStream var10000 = super.in;
  131.       return ((long)this.readInt() << 32) + ((long)this.readInt() & 4294967295L);
  132.    }
  133.  
  134.    public final short readShort() throws IOException {
  135.       InputStream in = super.in;
  136.       int ch1 = in.read();
  137.       int ch2 = in.read();
  138.       if ((ch1 | ch2) < 0) {
  139.          throw new EOFException();
  140.       } else {
  141.          return (short)((ch1 << 8) + (ch2 << 0));
  142.       }
  143.    }
  144.  
  145.    public final int readUnsignedByte() throws IOException {
  146.       int ch = super.in.read();
  147.       if (ch < 0) {
  148.          throw new EOFException();
  149.       } else {
  150.          return ch;
  151.       }
  152.    }
  153.  
  154.    public final int readUnsignedShort() throws IOException {
  155.       InputStream in = super.in;
  156.       int ch1 = in.read();
  157.       int ch2 = in.read();
  158.       if ((ch1 | ch2) < 0) {
  159.          throw new EOFException();
  160.       } else {
  161.          return (ch1 << 8) + (ch2 << 0);
  162.       }
  163.    }
  164.  
  165.    public final String readUTF() throws IOException {
  166.       return readUTF(this);
  167.    }
  168.  
  169.    public static final String readUTF(DataInput in) throws IOException {
  170.       int utflen = in.readUnsignedShort();
  171.       char[] str = new char[utflen];
  172.       int count = 0;
  173.       int strlen = 0;
  174.  
  175.       while(count < utflen) {
  176.          int c = in.readUnsignedByte();
  177.          switch (c >> 4) {
  178.             case 0:
  179.             case 1:
  180.             case 2:
  181.             case 3:
  182.             case 4:
  183.             case 5:
  184.             case 6:
  185.             case 7:
  186.                ++count;
  187.                str[strlen++] = (char)c;
  188.                break;
  189.             case 12:
  190.             case 13:
  191.                count += 2;
  192.                if (count > utflen) {
  193.                   throw new UTFDataFormatException();
  194.                }
  195.  
  196.                int var8 = in.readUnsignedByte();
  197.                if ((var8 & 192) != 128) {
  198.                   throw new UTFDataFormatException();
  199.                }
  200.  
  201.                str[strlen++] = (char)((c & 31) << 6 | var8 & 63);
  202.                break;
  203.             case 14:
  204.                count += 3;
  205.                if (count > utflen) {
  206.                   throw new UTFDataFormatException();
  207.                }
  208.  
  209.                int char2 = in.readUnsignedByte();
  210.                int char3 = in.readUnsignedByte();
  211.                if ((char2 & 192) == 128 && (char3 & 192) == 128) {
  212.                   str[strlen++] = (char)((c & 15) << 12 | (char2 & 63) << 6 | (char3 & 63) << 0);
  213.                   break;
  214.                }
  215.  
  216.                throw new UTFDataFormatException();
  217.             default:
  218.                throw new UTFDataFormatException();
  219.          }
  220.       }
  221.  
  222.       return new String(str, 0, strlen);
  223.    }
  224.  
  225.    public final int skipBytes(int n) throws IOException {
  226.       InputStream in = super.in;
  227.  
  228.       for(int i = 0; i < n; i += (int)in.skip((long)(n - i))) {
  229.       }
  230.  
  231.       return n;
  232.    }
  233. }
  234.